package com.zjcode.cdcer.service.impl;


import com.chinatechstar.component.commons.entity.MPage;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.service.BaseService;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjcode.cdcer.mapper.CerSysteminitImportInfoMapper;
import com.zjcode.cdcer.mapper.ZjcodeSelfTestInfoMapper;
import com.zjcode.cdcer.model.ZjcodeSelfTestInfo;
import com.zjcode.cdcer.service.*;
import com.zjcode.cdcer.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * (ZjcodeSelfTestInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-09-14 10:22:49
 */
@Service("zjcodeSelfTestInfoService")
public class ZjcodeSelfTestInfoServiceImpl implements ZjcodeSelfTestInfoService, BaseService {
    @Autowired
    private ZjcodeSelfTestInfoMapper zjcodeSelfTestInfoMapper;

    @Autowired
    private CerSysteminitImportInfoMapper cerSysteminitImportInfoMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private TkmSysUserService tkmSysUserService;

    @Autowired
    private TkmEndoscopeDiagnoseNounInfoService endoscopeDiagnoseNounInfoService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ZjcodeSelfTestInfo queryById(String id) {
        return this.zjcodeSelfTestInfoMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param zjcodeSelfTestInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */


    /**
     * 新增数据
     *
     * @param zjcodeSelfTestInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ZjcodeSelfTestInfo insert(ZjcodeSelfTestInfo zjcodeSelfTestInfo) {
        zjcodeSelfTestInfo.setDelFlag(new Byte("1"));
        zjcodeSelfTestInfo.setId(UuidUtil.get32UUID());
        this.zjcodeSelfTestInfoMapper.insert(zjcodeSelfTestInfo);
        return zjcodeSelfTestInfo;
    }

    /**
     * 修改数据
     *
     * @param zjcodeSelfTestInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ZjcodeSelfTestInfo update(ZjcodeSelfTestInfo zjcodeSelfTestInfo) {
        this.zjcodeSelfTestInfoMapper.update(zjcodeSelfTestInfo);
        return this.queryById(zjcodeSelfTestInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.zjcodeSelfTestInfoMapper.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> listAllData(PageData pageData) {
        String userId = (String) pageData.get("user");
        String extFlag = sysUserService.findUserExtFlag(userId);
        pageData.put("extFlag", extFlag);
        // 读取区域信息
        pageData = Utils.transitionRegionInfo(pageData);

        MPage mPage = getPageInfo(pageData);
        Page<Object> page = PageHelper.startPage(mPage.getCurrentPage(), mPage.getPageSize());
/*        List<LinkedHashMap<String, Object>> mapList = cerSysteminitImportInfoMapper.listAllTableData(
                pageData);*/
        List<LinkedHashMap<String, Object>> list = zjcodeSelfTestInfoMapper.listAllData(pageData);

        return PaginationBuilder.buildResult(list,
                page.getTotal(),
                mPage.getCurrentPage(),
                mPage.getPageSize());
    }

    @Override
    public String getIdByInstitutionIdAndIdCard(String institutionId, String idCard) {
        return  zjcodeSelfTestInfoMapper.queryByInstitutionIdAndIdCard(institutionId,idCard);
    }
}
